<?php
namespace app\admin\controller;
use app\service\serviceService;
use think\facade\Db;
use think\facade\View;

class Service extends Common{
    public function serviceList(){
        //查询服务商信息
        $list = (new serviceService())->serviceList();
        View::assign('list',$list);
        //查询服务商数据条数
        $number = (new serviceService())->serviceNumber();
        $number = count($number);
        View::assign('number',$number);
        return View::fetch('service_list');
    }
    
    
    public function serviceAdd(){
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            //验证表单信息是否符合
            $validate=  $this->validate($data,[
                    'name|服务商名称' => 'require',
                    'contacts|联系人' => 'require',
                    'phone|联系电话' => 'require',
                    'address|联系地址' => 'require',
                    'region|区域' => 'require',
                    
            ]);
            if($validate !== true){
                $this->error($validate);
            }
            //判断账号名称是否重复        
            $info = (new serviceService())->serviceName($data['name']);
            if($info){
                $this->error('服务商已存在');
            }
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new serviceService())->serviceAdd($data);
            if($result){
                $this->success('添加服务商成功',url('serviceList'));
            }else{
                $this->error('添加服务商失败',url('serviceAdd'));
            }             
        }
        return View::fetch('service_add');
    }
    public function serviceEdit(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('服务商不存在');
            }
            $info = (new serviceService())->serviceId($id);
            View::assign('info',$info);
            return View::fetch('service_edit');
        }else{
            $data = request()->post();
            $validate=  $this->validate($data,[
                'name|服务商名称' => 'require',
                'contacts|联系人' => 'require',
                'phone|联系电话' => 'require',
                'address|联系地址' => 'require',
                'region|区域' => 'require',
            ]);
            if($validate !== true){
                    $this->error($validate);
            }
            $result = (new serviceService())->serviceUpdate($data);
            if($result){
                    $this->success('修改成功',url('serviceList'));
            }else{
                    $this->error('修改失败');
            }
        }
    }
    public function serviceDel(){
        $id = request()->param('id');
		$validate = $this->validate(request()->param(),[
			'id'	=>	'require|number'
		]);
		if($validate !== true){
			$this->error($validate);
        }
        //判断是否存在该数据
		$info = (new serviceService())->serviceId($id);
		if(empty($info)){
			$this->error('该服务商不存在');
		}
		$result = Db::name('service')->delete($id);
        if($result){
            $this->success('删除服务商成功',url('serviceList'));
		}else{
            $this->success('删除服务商失败',url('serviceList'));
		}
    }
    
    public function serviceSet(){
         //查询服务商信息
        $list = (new serviceService())->serviceList();
        View::assign('list',$list);
        //查询服务商数据条数
        $number = (new serviceService())->serviceNumber();
        $number = count($number);
        View::assign('number',$number);
        return View::fetch('service_set');
       
    }
    
    public function invoice(){
        if(request()->isGet()){
            $id = request()->param('id');
            if(!isset($id) && empty($id)){
                $this->error('服务商不存在');
            }
            $info = (new serviceService())->serviceId($id);
            View::assign('info',$info);
            return View::fetch('service_invoice');
        }else{
            $data = request()->post();
            
            $result = (new serviceService())->serviceUpdate($data);
            if($result){
                return "发票信息配置成功";
                    $this->success('修改成功',url('serviceList'));
            }else{
                //return "发票信息配置失败";
                 $this->error('发票信息配置失败');
            }
        }
    }
}